package org.lanqiao.netctoss.controller.admin;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lanqiao.netctoss.entity.Admin;
import org.lanqiao.netctoss.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class ChangePsw
 */
public class ChangePswServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangePswServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("GBK");		
		HttpSession session = request.getSession();				
		String oldpsw = request.getParameter("oldpsw");
		String psw1 = request.getParameter("psw1");
		String psw2 = request.getParameter("psw2");
		Admin admin = (Admin)session.getAttribute("admin");
		String user =  (String) session.getAttribute("user");
		String psw =  (String) session.getAttribute("psw");
		PrintWriter out = response.getWriter();		
		if((psw.equals(oldpsw))&&(psw1.equals(psw2))){
			/*boolean b = adminBuss.updPsw(new Admin(user,psw1));*/
			int i = new AdminServiceImpl().updatePsw(new Admin(user,psw1));
			if(i>0){
				out.print("<script>alert('恭喜您，修改成功!');window.location.href='../view/login.jsp'</script>");
				/*Admin admin = (Admin)session.getAttribute("admin");
				admin.setAdm_tel(tel);
				admin.setAdm_name(name);
				admin.setAdm_email(email);*/
			}else{
				out.print("<script>alert('修改失败!');window.location.href='view/user/user_modi_psw.jsp'</script>");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
